package com.chenjl.transport.codec;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.chenjl.serialization.Serialization;
import com.chenjl.serialization.support.HessianSerialization;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
/**
 * 对象解码器
 * 2016-8-30 18:33:00
 * @author chenjinlong
 */
public class ObjectDecoder extends ByteToMessageDecoder {
	private static final Logger logger = LoggerFactory.getLogger(ObjectDecoder.class);
	private Serialization serialization = new HessianSerialization();
	
	@Override
	protected void decode(ChannelHandlerContext ctx, ByteBuf in,List<Object> out) throws Exception {
		logger.info("decode , hashcode : {}",this.hashCode());
		
		int readable = in.readableBytes();
		if (readable <= 0) {
			logger.warn("readable <=0 , value = {}",readable);
			return;
		}
        
		byte[] bytes = new byte[readable];
		in.readBytes(bytes);
		
		Object object = serialization.toObject(bytes);
		out.add(object);
	}
}